package fyp.spedmew.connector;

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

import java.io.IOException;

import fyp.spedmew.backend.entity.checkInEndpoint.CheckInEndpoint;
import fyp.spedmew.backend.entity.jobEndpoint.JobEndpoint;
import fyp.spedmew.backend.entity.userEndpoint.UserEndpoint;
import fyp.spedmew.util.Constant;

/**
 * Created by Andy on 22/12/2014.
 */
public class ApiService {
    private static JobEndpoint jobEndpoint = null;
    private static UserEndpoint userEndpoint = null;
    private static CheckInEndpoint checkInEndpoint = null;

    private static final String ROOT_URL = "http://"+ Constant.APP_ADDRESS + ":" + Constant.APP_PORT + "/_ah/api/";

    static public JobEndpoint.JobEndpointOperations getJobEndpoint() {
        if (jobEndpoint == null) {
            JobEndpoint.Builder jobBuilder = new JobEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                    // options for running against local devappserver
                    // - turn off compression when running against local devappserver
                    .setRootUrl(ROOT_URL)
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });

            jobEndpoint = jobBuilder.build();

        }
        return jobEndpoint.jobEndpoint();
    }

    static public UserEndpoint.UserEndpointOperations getUserEndpoint() {
        if (userEndpoint == null) {
            UserEndpoint.Builder userBuilder = new UserEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                    // options for running against local devappserver
                    // - turn off compression when running against local devappserver
                    .setRootUrl(ROOT_URL)
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });
            userEndpoint = userBuilder.build();
        }
        return userEndpoint.userEndpoint();
    }

    static public CheckInEndpoint.CheckInEndpointOperations getCheckInEndpoint() {
        if (checkInEndpoint == null) {
            CheckInEndpoint.Builder userBuilder = new CheckInEndpoint.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                    // options for running against local devappserver
                    // - turn off compression when running against local devappserver
                    .setRootUrl(ROOT_URL)
                    .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                        @Override
                        public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                            abstractGoogleClientRequest.setDisableGZipContent(true);
                        }
                    });
            checkInEndpoint = userBuilder.build();
        }
        return checkInEndpoint.checkInEndpoint();
    }
}
